#!/usr/bin/env python
"""
# -*- coding: utf-8 -*-
# @Time    : 2020/9/27 12:52
# @Author  : AshJo
# @Desc    : 学生选课系统
# @Filename: Corese_System.py
# @Version : v1.0
"""

import pickle
import sys

USERINFO = r'D:\VScode\Python\Project\Python_fullstack\day27\userinfo'
STUINFO = r'D:\VScode\Python\Project\Python_fullstack\day27\stuinfo'
COURSEINFO = r'D:\VScode\Python\Project\Python_fullstack\day27\courseinfo'

flag = True


class Course(object):
    def __init__(self, course, price, period):
        self.course = course
        self.price = price
        self.period = period


class Student(object):
    choose_lst = [('查看已选课程', 'view_selected'),
                  ('选择课程', 'choose_course'), ('查看所有课程', 'view_courses'),
                  ('退出', 'exit')]

    def __init__(self, name):
        self.name = name
        self.lst = []


    def view_courses(self):
        with open(COURSEINFO, mode='rb') as f:
            while True:
                try:
                    obj = pickle.load(f)
                    print(obj.course, obj.price, obj.period)
                except EOFError:
                    break

    def choose_course(self):
        pass

    def view_selected(self):
        pass

    def exit(self):
        return False
        exit(1)

    @staticmethod
    def init(ret):
        with open(STUINFO, mode='rb') as f:
            while True:
                try:
                    obj = pickle.load(f)
                    if obj.name == ret[0]:
                        return obj
                except EOFError:
                    print('没有找到%s这个学生' % (obj.name))


class Manager(object):
    choose_lst = [('创建课程', 'create_course'), ('查看所有课程', 'view_courses'),
                  ('创建学生账户', 'create_student'), ('查看所有学生', 'view_students'),
                  ('查看学生选课情况', 'check_student_selection'), ('退出', 'exit')]

    def __init__(self, name):
        self.name = name

    def create_course(self):
        name = input('课程名：')
        price = int(input('价格：'))
        period = int(input('周期：'))
        course = Course(name, price, period)
        with open(COURSEINFO, mode='ab') as f:
            pickle.dump(course, f)
        print('课程 %s 创建成功！' % (course.course))

    def create_student(self):
        usr = input('创建学生用户名：')
        stu = Student(usr)
        with open(USERINFO, encoding='utf-8', mode='a') as f:
            f.write('%s|123456|Student\n' % (usr))
        with open(STUINFO, mode='ab') as f:
            pickle.dump(stu, f)
        print('学生 %s 创建成功！' % (stu.name))

    def view_courses(self):
        with open(COURSEINFO, mode='rb') as f:
            while True:
                try:
                    obj = pickle.load(f)
                    print(obj.course, obj.price, obj.period)
                except EOFError:
                    break

    def view_students(self):
        with open(STUINFO, mode='rb') as f:
            while True:
                try:
                    obj = pickle.load(f)
                    print(obj.name)
                except EOFError:
                    break

    def check_student_selection(self):
        pass

    def exit(self):
        return False
        exit(1)

    @classmethod
    def init(cls, ret):
        obj = cls(ret[0])
        return obj


def login():
    """

    :return: 登录成功返回 用户名 身份;登录失败返回 False
    """
    username = input('用户名：')
    password = input('密码：')
    with open(USERINFO, encoding='utf-8') as f:
        for line in f:
            usr, pwd, ident = line.strip().split('|')
            if usr == username and pwd == password:
                return usr, ident
        else:
            return False


def main():
    ret = login()
    if ret:
        print('登录成功！%s 欢迎使用选课系统' % (ret[0]))
        cls = getattr(sys.modules[__name__], ret[1])
        obj = cls.init(ret)
        for index, item in enumerate(cls.choose_lst, 1):
            print(index, item[0])
        num = int(input('>>>').strip())
        if hasattr(obj, cls.choose_lst[num - 1][1]):
            getattr(obj, cls.choose_lst[num - 1][1])()
    else:
        print('登录失败，请检查！')


if __name__ == '__main__':
    main()
